package com.mingxingyi.base.pojo;

import com.mingxingyi.base.constant.ResultConstant;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
public class ResultVO<T> {

    private int code = ResultConstant._OK;

    private String message = "success";

    private T data;

    private int total;

    private List<T> list;

    public static <T> ResultVO<T> success() {
        return success(ResultConstant._OK, null, null);
    }

    public static <T> ResultVO<T> success(List<T> data) {
        if (data == null) data = new ArrayList<>();
        return successList(ResultConstant._OK, null, data);
    }

    private static <T> ResultVO<T> successList(int code, String message, List<T> data) {
        ResultVO<T> r = new ResultVO<>();
        r.setCode(code);
        r.setMessage(message);

        r.setList(data);
        return r;
    }

    public static <T> ResultVO<T> success(T data) {
        return success(ResultConstant._OK, null, data);
    }

    public static <T> ResultVO<T> fail(String message) {
        return fail(ResultConstant._ERROR, message, null);
    }

    public static <T> ResultVO<T> fail(String code, String message) {
        return fail(ResultConstant._ERROR, message, null);
    }

    public static <T> ResultVO<T> success(int code, String message, T data) {
        ResultVO<T> r = new ResultVO<>();
        r.setCode(code);
        r.setMessage(message);
        r.setData(data);

        return r;
    }

    public static <T> ResultVO<T> fail(int code, String message) {
        return fail(code, message, null);
    }

    public static <T> ResultVO<T> fail(int code, String message, T data) {
        ResultVO<T> r = new ResultVO<>();
        r.setCode(code);
        r.setMessage(message);
        r.setData(data);
        return r;
    }

}
